package com.chongba.schedule.jdk;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class TimerTaskTest2 {
    private static Long st=System.currentTimeMillis()/1000;
    public static void main(String[] args) {
        Timer timer = new Timer();
        /*for (int i = 0; i < 100; i++) {
            int finalI = i;
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    System.out.println(finalI);
                    if (finalI == 20){
                        throw new RuntimeException();
                    }
                }
            },1000L);
        }*/
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5);


        /*for (int i = 0; i < 100; i++) {
            int finalI = i;
            executorService.schedule(new TimerTask() {
                @Override
                public void run() {
                    System.out.println(finalI);
                    if (finalI == 20){
                        throw new RuntimeException();
                    }
                }
            },1, TimeUnit.SECONDS);
        }*/
        TimerTask timerTask1 = new TimerTask() {
            @Override
            public void run() {
                System.out.println(System.currentTimeMillis()/1000-st);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        TimerTask timerTask2 = new TimerTask() {
            @Override
            public void run() {
                System.out.println(System.currentTimeMillis()/1000-st);
            }
        };
        executorService.schedule(timerTask1,1, TimeUnit.SECONDS);
        executorService.schedule(timerTask2,4,TimeUnit.SECONDS);
    }
}
